window.addEvent('domready', function(){
	if($('seriesUpdate'))
	$('seriesUpdate').addEvent('change', function(e){
		$('seriesUpdateF').submit();
		this.options[0].selected = true;
	});
	
	$$('.del').addEvent('click', function(e){
		if(!confirm('Biztosan végrehajtod a kiválasztott műveletet?'))
			e.preventDefault();
	});
	
	(function(){
		if($$('.box > h2').length < 3)
			return false;
		var toc = $('TOC');	
		
		$$('.box > h2').each(function(elem){
			var id = elem.get('text').removeAccents().toLowerCase();
			elem.set('id', id);
			var li = new Element('li').inject(toc);
			new Element('a', {text: elem.get('text'), href: '#' + id}).inject(li);
		});
	})();
	
	$$('.checkableTable').each(function(elem){
		new checkableTable(elem);
	});
	
	new scrollToTop();
	
	new Fx.SmoothScroll({
		links: $$('a[href*="#"]'),
		wheelStops: false
	});
});

var scrollToTop = new Class({
	initialize: function(){
		this.toTop = new Element('a', {
			text: 'Tetejére',
			href: '#top',
			styles: {
				opacity: 0,
				visibility: 'hidden'
			}
		}).inject($(document.body));
		
		this.bodyScroll = new Fx.Scroll($(document.body));
		
		this.toTop.addEvent('click', function(e){
			e.preventDefault();		
			this.bodyScroll.toTop();		
		}.bind(this));
		
		window.addEvent('scroll', this.changeVisibility.bind(this));
	},
	changeVisibility: function(){
		if(!this.toTop.getStyle('opacity') && $(document.body).getScroll().y > 200)
		{
			this.toTop.fade('show');
		}
		else if(this.toTop.getStyle('opacity')  && $(document.body).getScroll().y < 200) {
			this.toTop.fade('hide');
		}
	}
});

var checkableTable = new Class({
	initialize: function(table){
		this.table = table;
		this.table.ondragstart = function(){ return false; };
		this.loadUp();
	},
	loadUp: function(){
		this.tableRows = this.table.getElements('tbody > tr');
		var active = 0;	
		this.tableRows.each(function(elem){		
			var chbox = elem.getElement('input[type="checkbox"]');
			if(!chbox)
				return;
			elem.addEvents({
				mousedown: function(e){
					if(e.rightClick)		
						return;
					var ttag = e.target.get('tag');					
					if(ttag == 'td' || ttag == 'th' || ttag == 'tr')
					{
						e.preventDefault();
						active = 1;
						chbox.checked = !chbox.checked;
					}
				},
				mouseenter: function(e){
					var ttag = e.target.get('tag');
					if(active === 1 && (ttag == 'td' || ttag == 'th' || ttag == 'tr'))
						chbox.checked = !chbox.checked;
				}
			});			
		});
		window.addEvent('mouseup', function() { active = 0; });
	}
});

String.implement({
	removeAccents : function(){
		var str = this;
		
		/**
			Kisbetűk
		**/
		
		str = str.replace(/á/g, 'a');
	
		str = str.replace(/é/g, 'e');
	
		str = str.replace(/í/g, 'i');

		str = str.replace(/ö/g, 'o');
	
		str = str.replace(/ü/g, 'u');
	
		str = str.replace(/ó/g, 'o');

		str = str.replace(/ő/g, 'o');
	
		str = str.replace(/ú/g, 'u');
	
		str = str.replace(/ű/g, 'u');

		/**
			Nagybetűk
		**/
		
		str = str.replace(/Á/g, 'A');
	
		str = str.replace(/É/g, 'E');
	
		str = str.replace(/Í/g, 'I');

		str = str.replace(/Ö/g, 'O');
	
		str = str.replace(/Ü/g, 'U');
	
		str = str.replace(/Ó/g, 'O');

		str = str.replace(/Ő/g, 'O');
	
		str = str.replace(/Ú/g, 'U');
	
		str = str.replace(/Ű/g, 'U');
	
		str = str.replace(/\W/g, '');

		return str;
	}
});